package com.base.util;

import java.io.File;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletWebRequest;

import com.base.common.CommonConstants;
import com.base.exception.RuntimeMessageException;
import com.base.pojo.Sign;






final public class JUtil {
	
	
	/**
	 * 默认值
	 * 
	 * @param obj
	 * @param defVal
	 * @return
	 */
	public static Object substr(String str, int length, String end) {
		if (null == str) {
			return "";
		}
		
		if (str.length() <= length) {
			return str;
		}
		
		float charlenth = 0;
		char[] chars = str.toCharArray();
		int idx = 0;
		for (char c : chars) {
			int len = getLength(c);
			if (len > 1) {
				charlenth += 1;
			} else {
				charlenth += 0.5;
			}
			if (charlenth >= length) {
				int sub_len = charlenth == length ? idx + 1 : idx;
				return str.substring(0, sub_len) + end;
			}
			
			idx++;
			// 4 -1 -1
			// 啊是双调是xxxxx
			// if(length < str.length())break;
		}
		
		// 英文字母/数字/2位算1位
		return str;
	}
	
	public static Method getMethod(Class myClass, String methodName, Class... values) {
		Method theMethod = null;
		try {
			theMethod = myClass.getDeclaredMethod(methodName, values);
		} catch (NoSuchMethodException e) {
			theMethod = getMethod(myClass.getSuperclass(), methodName, values);
		}
		return theMethod;
	}
	
	//首字母大写
	public static String getFirstWordUppercase(String name) {
		return  name.substring(0, 1).toUpperCase() + name.substring(1);
	}
	
	public static String getGetMethod(String name) {
		return "get" + getFirstWordUppercase(name);
	}
	
	public static String setGetMethod(String name) {
		return "set" +getFirstWordUppercase(name);
	}
	
	public static void main(String[] args) {
		/*
		 * String result4 = autoSpace( 16 ,new
		 * AutoLine(12,"","预点号∶",AutoLine.LEFT,12f) ,new
		 * AutoLine(4,"","1245",AutoLine.LEFT,4f) ); System.out.println("-" +
		 * result4 + "-");
		 * 
		 * String result = autoSpace( 16 ,new
		 * AutoLine(5,"**","菜名",AutoLine.LEFT,5.5f) ,new
		 * AutoLine(4,"","数量",AutoLine.LEFT,4) ,new
		 * AutoLine(7,"","价格",AutoLine.LEFT,7.5f) ); System.out.println("-" +
		 * result + "-");
		 * 
		 * String result2 = autoSpace( 16 ,new
		 * AutoLine(5,"**","超级大卤肉饭",AutoLine.RIGHT,5.5f) ,new
		 * AutoLine(4,"","88",AutoLine.LEFT,4) ,new
		 * AutoLine(7,"","99.78",AutoLine.LEFT,7.5f) ); System.out.println("-" +
		 * result2 + "-");
		 * 
		 * 
		 * for (int i = 0; i < 4; i++) { String result3 = autoSpace( 16 ,new
		 * AutoLine(5,"*","白菜",AutoLine.RIGHT,5.5f) ,new
		 * AutoLine(4,"","88",AutoLine.LEFT,4) ,new
		 * AutoLine(7,"","9922.70",AutoLine.LEFT,7.5f) ); System.out.println("-"
		 * + result3 + "-"); }
		 */
		
		try {
			//System.out.println(JUtil.getLoanCode(150));
			Date d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-09-19 07:00:00");
			int age = getAge(d2.getTime());
			System.out.println(age);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	//计算相差的年数
	public static int getAge(long birth){
		long end = new Date().getTime();
		int age = (int) (((end - birth)/(24 * 60 * 60 * 1000))/365);
		System.out.println(age);
		return age;
	}
	
	/**
	 * 
	 * @param lineMax
	 *            每行最多的字符个数
	 * @param lines
	 * @return
	 */
	public static String autoSpace(int lineMax, AutoLine... lines) {
		StringBuilder strb = new StringBuilder();
		for (AutoLine line : lines) {
			Object strResult = substr(line.getStr(), line.getMaxsize(), line.getCha());
			if (strResult instanceof String) {
				line.setConvertStr((String) strResult);
			} else {
				line.setConvertStr("");
			}
			float convertLenth = getLength(line.getConvertStr());
			float expectLength = line.getExpectLength();
			// 计算长度
			line.setConvertLength(convertLenth);
			// 如果实际长度小于预期长度
			if (convertLenth < expectLength) {
				String nbsps = genNbsp((float) expectLength - convertLenth);
				if (line.getDirection() == AutoLine.LEFT) {
					strb.append(nbsps);
					strb.append(line.getConvertStr());
				} else if (line.getDirection() == AutoLine.RIGHT) {
					strb.append(line.getConvertStr());
					strb.append(nbsps);
				}
			} else if (convertLenth > expectLength) {
				strb.append(line.getConvertStr());
			} else {
				strb.append(line.getConvertStr());
			}
		}
		
		/*
		 * for (AutoLine line : lines) { //开始拼装字符串
		 * strb.append(line.getConvertStr()); }
		 */
		
		return strb.toString();
	};
	
	/**
	 * 
	 * @param sum
	 * @return
	 */
	public static String genNbsp(float sum) {
		StringBuilder nbsps = new StringBuilder();
		int intval = new Float(sum).intValue();
		int loopSum = intval;
		if (sum > intval) {
			loopSum += 1;
		}
		
		for (int i = 0; i < loopSum; i++) {
			nbsps.append("　");
		}
		return nbsps.toString();
	};
	
	public static class AutoLine {
		public static final int LEFT = 0;
		
		public static final int RIGHT = 1;
		
		/**
		 * 
		 * @param maxsize
		 *            超过多少长度加省略字符
		 * @param cha
		 *            省略字符
		 * @param str
		 *            字符串
		 * @param direction
		 *            方向
		 * @param expectLength
		 *            预期最长长度(必须要给每个区块设置好预期长度)
		 */
		public AutoLine(int maxsize, String cha, String str, int direction, float expectLength) {
			super();
			this.maxsize = maxsize;
			this.cha = cha;
			this.str = str;
			this.direction = direction;
			this.expectLength = expectLength;
		}
		
		// 补充空格的方位
		int direction;
		
		// 最大长度
		private int maxsize;
		
		// 补充字符
		private String cha;
		
		// 字符串
		private String str;
		
		// 转换后的字符串
		private String convertStr;
		
		// 转换后的字符串
		private float convertLength;
		
		// 预期最长长度(必须要给每个区块设置好预期长度)
		private float expectLength;
		
		public int getMaxsize() {
			return maxsize;
		}
		
		public void setMaxsize(int maxsize) {
			this.maxsize = maxsize;
		}
		
		public String getCha() {
			return cha;
		}
		
		public void setCha(String cha) {
			this.cha = cha;
		}
		
		public String getStr() {
			return str;
		}
		
		public void setStr(String str) {
			this.str = str;
		}
		
		public String getConvertStr() {
			return convertStr;
		}
		
		public void setConvertStr(String convertStr) {
			this.convertStr = convertStr;
		}
		
		public float getConvertLength() {
			return convertLength;
		}
		
		public void setConvertLength(float convertLength) {
			this.convertLength = convertLength;
		}
		
		public float getExpectLength() {
			return expectLength;
		}
		
		public void setExpectLength(float expectLength) {
			this.expectLength = expectLength;
		}
		
		public int getDirection() {
			return direction;
		}
		
		public void setDirection(int direction) {
			this.direction = direction;
		}
	}
	
	/**
	 * 得到字符串的长度
	 * 
	 * @param str
	 * @return
	 */
	public static float getLength(String str) {
		float charlenth = 0;
		char[] chars = str.toCharArray();
		for (char c : chars) {
			int len = getLength(c);
			if (len > 1) {
				charlenth += 1;
			} else {
				charlenth += 0.5;
			}
		}
		return charlenth;
	}
	
	/**
	 * 得到字符的长度
	 * 
	 * @param c
	 * @return
	 */
	public static int getLength(char c) {
		Pattern patt = Pattern.compile("^[\u4e00-\u9fa5]{1}$");
		Matcher matcher = patt.matcher(new String(new char[] { c }));
		if (matcher.find()) {
			return 2;
		}
		return 1;
	}
	
	/**
	 * 默认值
	 * 
	 * @param obj
	 * @param defVal
	 * @return
	 */
	public static Object roundDouble(Double obj) {
		if (null == obj)
			return 0;
		return Math.round(obj);
	}
	
	/**
	 * 默认值
	 * 
	 * @param obj
	 * @param defVal
	 * @return
	 */
	public static Object def(Object obj, Object defVal) {
		if (null == obj)
			return defVal;
		return obj;
	}
	
	/**
	 * 
	 * @param obj
	 * @param defVal
	 * @return
	 */
	public static String star(Float val) {
		if (null == val)
			return "2_5";
		String res = "";
		if (val > 4.5d) {
			res = "5";
		} else if (val > 4d) {
			res = "4_5";
		} else if (val > 3.5d) {
			res = "4";
		} else if (val > 3d) {
			res = "3_5";
		} else if (val > 2.5d) {
			res = "3";
		} else if (val > 2d) {
			res = "2_5";
		} else if (val > 1.5d) {
			res = "2";
		} else if (val > 1d) {
			res = "1_5";
		} else if (val > 0.5d) {
			res = "1";
		} else if (val > 0d) {
			res = "0_5";
		} else {
			res = "0_5";
		}
		return res;
	}
	
	/**
	 * 构建Select
	 * 
	 * @param array
	 * @param text
	 * @param value
	 * @return
	 */
	public static List<Map<String, Object>> builderSelect(JSONArray array, String textField, String valueField) {
		List<Map<String, Object>> res = new ArrayList<Map<String, Object>>();
		if (null == array) {
			return res;
		}
		int size = array.size();
		for (int i = 0; i < size; i++) {
			JSONObject jsonObj = array.getJSONObject(i);
			if (null == jsonObj) {
				continue;
			}
			Map<String, Object> option = new HashMap<String, Object>();
			option.put("text", jsonObj.get(textField));
			option.put("value", jsonObj.get(valueField));
			res.add(option);
		}
		
		return res;
	}
	
	/**
	 * 封装快框的formatDatagirdData方法
	 * 
	 * @param array
	 * @param page
	 * @return
	 */
	public static HashMap<String, Object> formatDatagirdData(Object array, Page page) {
		HashMap<String, Object> cacheData = new HashMap<String, Object>();
		cacheData.put("data", array);
		HashMap<String, Object> info = new HashMap<String, Object>();
		if (null != page) {
			info.put("total", page.getCount());// 总条数
			info.put("page", page.getCurrent());// 当前页面
			info.put("records", page.getTotal());// 总页数
			info.put("rows", page.getLength());// 每页显示多少条
		} else {
			info.put("total", 0);// 总条数
			info.put("page", 0);// 当前页面
			info.put("records", 0);// 总页数
			info.put("rows", 0);// 每页显示多少条
		}
		cacheData.put("info", info);
		return cacheData;
	}
	
	/**
	 * 添加前缀，map
	 * 
	 * @Title: addPrefix
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param map
	 * @param prefix
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Map addPrefix(Map map, String prefix) {
		Map res = new HashMap();
		if (null == map)
			return res;
		Set<String> keys = map.keySet();
		for (String key : keys) {
			Object val = map.get(key);
			res.put(prefix + key, val);
			// map.remove(key);
		}
		return res;
	}
	
	public static List<Integer> splitToListInteger(String fieldIdStr) throws Exception {
		return splitToListInteger(fieldIdStr, ",");
	}
	
	public static List<Integer> splitToListInteger(String fieldIdStr, String character) throws Exception {
		String[] chars = fieldIdStr.split(character);
		List<Integer> listChars = new ArrayList<Integer>();
		for (String cha : chars) {
			Integer i = strToInt(cha);
			listChars.add(i);
		}
		return listChars;
	}
	
	public static List<String> splitToListString(String fieldIdStr) {
		return splitToListString(fieldIdStr, ",");
	}
	
	/**
	 * 
	 * @param fieldIdStr
	 * @param character
	 * @return
	 */
	public static List<String> splitToListString(String fieldIdStr, String character) {
		String[] chars = fieldIdStr.split(character);
		List<String> listChars = new ArrayList<String>();
		for (String cha : chars) {
			listChars.add(cha);
		}
		return listChars;
	}
	
	public static String splitListToString(List<String> strs) {
		return splitListToString(strs, ",");
	}
	
	
	
	//将List<Integer>转化成string
	public static String splitListIntToString(List<Integer> strs) {
		return splitListIntToString(strs, ",");
	}
	
	/**
	 * 
	 * @param fieldIdStr
	 * @param character
	 * @return
	 */
	public static String splitListIntToString(List<Integer> strs, String character) {
		StringBuilder sbui = new StringBuilder();
		int size = strs.size();
		int index = 0;
		for (Integer str : strs) {
			sbui.append(str);
			if ((++index) < size) {
				sbui.append(",");
			}
		}
		return sbui.toString();
	}
	
	/**
	 * 
	 * @param fieldIdStr
	 * @param character
	 * @return
	 */
	public static String splitListToString(List<String> strs, String character) {
		StringBuilder sbui = new StringBuilder();
		int size = strs.size();
		int index = 0;
		for (String str : strs) {
			sbui.append(str);
			if ((++index) < size) {
				sbui.append(",");
			}
		}
		return sbui.toString();
	}
	
	public static boolean isMorethanNhours(Date startDate, Date endDate, double nhours) {
		// 计算时间差long
		long diff = endDate.getTime() - startDate.getTime();
		double hours = diff / (double) (1000 * 60 * 60);
		// System.out.println(hours);
		return hours > nhours;
	}
	
	/**
	 * 得到出差天数
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static double getTravelDays(Date startDate, Date endDate, double days) throws ParseException {
		// 出发时间(上午或下午)
		int startAmOrPm = JUtil.getAmOrPm(startDate);
		// 到达时间(上午或下午)
		int endAmOrPm = JUtil.getAmOrPm(endDate);
		
		// 当天出发当天到
		if (days == 0) {
			double[][] dayflag = new double[2][2];
			dayflag[JUtil.AM][JUtil.PM] = 1;
			dayflag[JUtil.AM][JUtil.AM] = 0.5;
			dayflag[JUtil.PM][JUtil.PM] = 0.5;
			days += dayflag[startAmOrPm][endAmOrPm];
		} else {// 当天出发隔n天到
			double[][] dayflag = new double[2][2];
			dayflag[JUtil.AM][JUtil.PM] = 2;
			dayflag[JUtil.AM][JUtil.AM] = 1.5;
			dayflag[JUtil.PM][JUtil.AM] = 1;
			dayflag[JUtil.PM][JUtil.PM] = 1.5;
			days += dayflag[startAmOrPm][endAmOrPm];
			days -= 1;
		}
		return days;
	}
	
	/**
	 * 得到天数差
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static double getDaysDifference(Date startDate, Date endDate) throws ParseException {
		// 时间只取到天
		startDate = getDate(startDate);
		endDate = getDate(endDate);
		// 计算时间差long
		long diff = endDate.getTime() - startDate.getTime();
		// long转换为天数
		// double days = diff / (1000 * 60 * 60 * 24);
		long res = diff / 86400000;
		return new Long(res).doubleValue();
	}
	
	public static boolean isNull(String filed) {
		return null == filed || "undefined".equals(filed.trim()) || "".equals(filed);
	}
	
	public static void checkNull(String filed, String msg) {
		if (isNull(filed))
			throw new RuntimeException(msg);
	}
	
	public static void checkNull(String filed, RuntimeException e) {
		if (isNull(filed))
			throw e;
	}
	
	public static void checkNULL(Object filed, String msg) {
		if (null == filed)
			throw new RuntimeException(msg);
	}
	
	public static void checkNULL(Object filed, RuntimeException e) {
		if (null == filed)
			throw e;
	}
	
	public static void checkNull2(String filed, String msg) {
		checkNULL(filed, new RuntimeMessageException(msg));
	}
	
	public static void checkNULL2(Object filed, String msg) {
		checkNULL(filed, new RuntimeMessageException(msg));
	}
	
	public static void checkNullArray(List array, String msg) {
		checkNULL(array, msg);
		if (array.isEmpty())
			throw new RuntimeException(msg);
	}
	
	public static boolean isNULL(Object filed) {
		return null == filed || "undefined".equals(filed.toString().trim()) || "".equals(filed.toString().trim());
	}
	
	/**
	 * @Title: getDate
	 * @Description: TODO
	 * @return
	 * @throws ParseException
	 */
	public static String getCurrentYearMonth() throws ParseException {
		return getCurrentDate("yyyy-MM");
	}
	
	/**
	 * @Title: getDate
	 * @Description: TODO
	 * @return
	 * @throws ParseException
	 */
	public static String getCurrentDate() throws ParseException {
		return getCurrentDate("yyyy-MM-dd");
	}
	
	/**
	 * @Title: getDate
	 * @Description: TODO
	 * @return
	 * @throws ParseException
	 */
	public static String getCurrentDateHHmmss() throws ParseException {
		return getCurrentDate("yyyy-MM-dd HH:mm:ss");
	}
	
	public static String getDateStr(String source, String format) throws ParseException {
		Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(source);
		return getDateStr(date,format);
	}
	
	public static String timestampToStr(Timestamp time,String format) throws Exception {
		if(null != time && !"".equals(time)){
			Date date = new Date(time.getTime());
			return getDateStr(date,format);
		}
		return null;
	}
	
	
	public static String getCurrentDate(String format) throws ParseException {
		return getDateStr(new Date(), format);
	}
	
	public static String getDateStr(Date date, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	
	public static Date getDate(String date, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(date);
	}
	
	public static Date getDate(Date date) throws ParseException {
		return getDate(date, "yyyy-MM-dd");
	}
	
	public static Date getDate(Date date, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(sdf.format(date));
	}
	
	public static final int AM = 0;// 上午
	
	public static final int PM = 1;// 下午
	
	public static final double SEMI_DAY = 0.5;// 半天
	
	public static final double ALL_DAY = 1;// 全天
	
	/**
	 * 得到上午或者下午
	 * 
	 * @param date
	 * @return AM/PM
	 * @throws ParseException
	 */
	public static int getAmOrPm(Date date) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 电子报账系统特殊需求
		return calendar.get(Calendar.HOUR_OF_DAY) > 11 ? PM : AM;
	}
	
	/**
	 * 是上午吗
	 * 
	 * @param date
	 * @return yes/no
	 * @throws ParseException
	 */
	public static boolean isAM(Date date) throws ParseException {
		return AM == getAmOrPm(date) ? true : false;
	}
	
	/**
	 * 是下午吗
	 * 
	 * @param date
	 * @return yes/no
	 * @throws ParseException
	 */
	public static boolean isPM(Date date) throws ParseException {
		return PM == getAmOrPm(date) ? true : false;
	}
	
	public static final boolean NO_WEEKEND = false;
	
	public static final boolean WEEKEND = true;
	
	/**
	 * 是否为周末
	 * 
	 * @param date
	 * @return AM/PM
	 * @throws ParseException
	 */
	public static boolean isWeekend(Date date) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// System.out.println(calendar.getTime());
		// calendar.add(Calendar.DAY_OF_WEEK, -2);
		// System.out.println(calendar.getTime());
		// 电子报账系统特殊需求
		// return calendar.get(Calendar.DAY_OF_WEEK) > 11 ? NO_WEEKEND :
		// WEEKEND;
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		return dayOfWeek == 1 || dayOfWeek == 7 ? WEEKEND : NO_WEEKEND;
	}
	
	/**
	 * 得到时间断的周末时间
	 * 
	 * @param date
	 * @return number
	 * @throws ParseException
	 */
	public static int getWeekendNum(Date startDate, Date endDate) throws ParseException {
		// 时间只取到天
		startDate = getDate(startDate);
		endDate = getDate(endDate);
		// 周木和
		int num = 0;
		Calendar start = Calendar.getInstance();
		start.setTime(startDate);
		Calendar end = Calendar.getInstance();
		end.setTime(endDate);
		// System.out.println(start.getTime());
		do {
			if (isWeekend(start.getTime())) {
				// System.out.println("周末:" + start.getTime());
				num++;
			}
			start.add(Calendar.DAY_OF_YEAR, 1);
		} while (start.before(end) || start.equals(end));
		return num;
	}
	
	public static double getFinalWeekendNum(Date startDate, Date endDate, double days) throws ParseException {
		double finalNum = getWeekendNum(startDate, endDate);
		// 如果开始时间是周末 并且 下午出发 扣半天
		if (isWeekend(startDate) && isPM(startDate)) {
			finalNum -= 0.5;
		}
		
		// 如果开始时间是周末 并且 上午出发 并且 出差时间为半天 扣半天
		if (isWeekend(startDate) && isAM(startDate) && SEMI_DAY == days) {
			finalNum -= 0.5;
		}
		
		// 如果结束时间是周末 并且 上午回 并且跨天 扣半天
		if (isWeekend(endDate) && isAM(endDate) && getDaysDifference(startDate, endDate) >= 1) {
			finalNum -= 0.5;
		}
		return finalNum;
	}
	
	/**
	 * @Title: defaultVal
	 * @Description: TODO
	 * @param o1
	 * @param o2
	 * @return
	 */
	public static <T> T defaultVal(T o1, T o2) {
		// TODO Auto-generated method stub
		return null == o1 ? o2 : o1;
	}
	
	/**
	 * 增删改 结果验证
	 * 
	 * @param flag
	 * @param msg
	 */
	public static void validCUD(int flag, String msg) {
		if (flag < 1) {
			throw new RuntimeException("数据库操作(" + msg + ")失败!");
		}
	}
	
	/**
	 * 首单词大写
	 * 
	 * @param str
	 * @return
	 */
	public static String upperFirstWord(String str) {
		// if(null == str || str.trim().length() < 1) return str;
		StringBuilder strb = new StringBuilder(str);
		strb.replace(0, 1, strb.substring(0, 1).toUpperCase());
		return strb.toString();
	}
	
	/**
	 * 首单词小写
	 * 
	 * @param str
	 * @return
	 */
	public static String lowerFirstWord(String str) {
		// if(null == str || str.trim().length() < 1) return str;
		StringBuilder strb = new StringBuilder(str);
		strb.replace(0, 1, strb.substring(0, 1).toLowerCase());
		return strb.toString();
	}
	
	// public static void main(String[] args) {
	/*
	 * try { Calendar s = Calendar.getInstance(); s.set(2012, 5, 17, 8,0,0);
	 * Calendar e = Calendar.getInstance(); e.set(2012, 5, 17, 12,0,0);
	 * //System.out.println(getWeekendNum(s.getTime(), e.getTime()));
	 * //System.out.println(getFinalWeekendNum(s.getTime(), e.getTime(),1.5));
	 * System.out.println(isMorethanNhours(s.getTime(), e.getTime(),4));
	 * System.out.println(getDaysDifference(s.getTime(), e.getTime()));
	 * //System.out.println(getDate(getCurrentDate(), "yyyy-MM-dd")); } catch
	 * (ParseException e) { e.printStackTrace(); }
	 */
	// long res = 28;
	// System.out.println((int) res);
	// System.out.println(UUNUM(12));
	// }
	
	
	
	public static String getSuffix(String fileName, String c) {
		if (null != fileName) {
			int lastindex = fileName.lastIndexOf(c);
			if (lastindex > -1) {
				return fileName.substring(fileName.lastIndexOf(c) + 1, fileName.length());
			}
		}
		return fileName;
	}
	
	public static <T> T valiObject(T t, String msg) {
		if (null == t) {
			throw new RuntimeException(msg);
		}
		return t;
	}
	
	
	public static String uuid() {
		return UUID.randomUUID().toString().replace("-", "");
	}
	
	public static String uuid12() {
		return uuid().substring(0, 12);
	}
	
	public static JSONObject getUser() {
		Object user = getSession().getAttribute("user");
		if (user instanceof JSONObject)
			return (JSONObject) user;
		return null;
	}
	
	public static double formatDouble(double f) {
		BigDecimal b = new BigDecimal(f);
		double f1 = b.setScale(2, BigDecimal.ROUND_FLOOR).doubleValue();
		return f1;
	}
	
	public static JSONObject getAdminUser() {
		return (JSONObject) getSession().getAttribute(CommonConstants.SESSION_SECURITY_CODE);
	}
	
	public static void setSessionObj(String key, Object value) {
		getSession().setAttribute(key, value);
	}
	
	public static void removeSessionObj(String key) {
		getSession().setAttribute(key, null);
	}
	
	public static Object getSessionObj(String key) {
		// TODO Auto-generated method stub
		return getSession().getAttribute(key);
	}
	
	public static HttpSession getSession() {
		return getRequest().getSession();
	}
	
	public static HttpSession getSession(HttpServletRequest request) {
		return request.getSession();
	}
	
	public static Object getSessionObject(HttpServletRequest request,String sign) {
		return getSession(request).getAttribute(sign);
	}
	
	public static Object getSessionObject(HttpServletRequest request) {
		return  getSession(request).getAttribute(CommonConstants.SESSION_MANAGE_USER);
	}
	
	public static void setSessionNull(HttpServletRequest request){
		getSession(request).setAttribute(CommonConstants.SESSION_MANAGE_USER, null);
	}
	
	public static HttpServletRequest getRequest() {
		ServletWebRequest servletContainer = (ServletWebRequest)RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = servletContainer.getRequest();
		HttpServletResponse response = servletContainer.getResponse();
		return request;
	}
	
	public static HttpServletResponse getResponse() {
		ServletWebRequest servletContainer = (ServletWebRequest)RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = servletContainer.getRequest();
		HttpServletResponse response = servletContainer.getResponse();
		return response;
	}
	
	public static String getDateSeq() {
		String dateSeq = "";
		try {
			dateSeq = getCurrentDate().replace("-", "");
		} catch (ParseException e) {
			dateSeq = "";
		}
		return dateSeq;
	}
	
	public static String getIp() {
		HttpServletRequest request = getRequest();
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null) {
			ip = request.getRemoteAddr();
		}
		if (null != ip && ip.equals("0:0:0:0:0:0:0:1")) {
			ip = "127.0.0.1";
		}
		return ip;
	}
	
	public static String getAreaId() {
		Object city = getSessionObj("city");
		if (!(city instanceof String)) {
			throw new CityNotFoundException();
		}
		return (String) city;
	}
	
	public static String UUNUM(int num) {
		StringBuilder strb = new StringBuilder();
		Random random = new Random();
		for (int j = 0; j < num; j++) {
			strb.append(Integer.toString(random.nextInt(10)));
		}
		return strb.toString();
	}
	
	public static boolean thanTime(Date endAddDatetime, Date date) {
		// TODO Auto-generated method stub
		if (null == endAddDatetime)
			return false;
		if (null == date)
			return false;
		return endAddDatetime.getTime() > date.getTime();
	}
	
	public static String getRootPath() {
		return "images/default/upload/prdimgs/" + JUtil.getDateSeq() + "/";
	}
	
	public static String getRootAudioPath(String id) {
		return "upload/audio/" + id + "/" + JUtil.getDateSeq() + "/";
	}
	
	public static String getAdvPath() {
		return "images/default/upload/adv/";
	}
	
	public static String getContextPath() {
		return getRequest().getContextPath();
	}
	
	public static String replaceNoECN(String s) {
		if (null == s)
			return null;
		return s.replaceAll("[^0-9a-zA-Z\u4e00-\u9fa5]", "");
	}
	
	public static class CityNotFoundException extends RuntimeException {
		
		public CityNotFoundException() {
			super();
		}
		
		public CityNotFoundException(String message, Throwable cause) {
			super(message, cause);
		}
		
		public CityNotFoundException(String message) {
			super(message);
		}
		
		public CityNotFoundException(Throwable cause) {
			super(cause);
		}
		
	}
	
	public static String getCurrentYear() {
		Date date = new Date();
		return getYear(date);
	}
	
	public static String getCurrentMonth() {
		Date date = new Date();
		return getMonth(date);
	}
	
	public static String getCurrentDay() {
		Date date = new Date();
		return getDay(date);
	}
	
	public static String getYear(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		return sdf.format(date);
	}
	
	public static String getMonth(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		return sdf.format(date);
	}
	
	public static String getDay(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
		return sdf.format(date);
	}
	
	/**
	 * 获取汉字拼音
	 * @param src
	 * @return
	 */
	public static String getPinYin(String src) {
		char[] t1 = null;
		t1 = src.toCharArray();
		String[] t2 = new String[t1.length];
		// 设置汉字拼音输出的格式
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
		t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);
		String t4 = "";
		int t0 = t1.length;
		try {
			for (int i = 0; i < t0; i++) {
				// 判断是否为汉字字符
				if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 将汉字的几种全拼都存到t2数组中
					t4 += t2[0];// 取出该汉字全拼的第一种读音并连接到字符串t4后
				} else {
					// 如果不是汉字字符，直接取出字符并连接到字符串t4后
					t4 += Character.toString(t1[i]);
				}
			}
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
		}
		return t4;
	}
	
	/**
	 * 获取折首个拼音
	 * @param cnchar
	 * @return
	 */
	public static String getPinyinFirst(String cnchar) {
		String pinyin = getPinYin(cnchar);
		return pinyin.substring(0, 1);
	}
	
	/**
	 * 更加经验获取级别
	 * @param exp
	 * @return
	 */
	public static Integer getLevel(Integer exp) {
		if (exp > 10000) {
			return 7;
		} else if (exp > 5000) {
			return 6;
		} else if (exp > 2500) {
			return 5;
		} else if (exp > 1250) {
			return 4;
		} else if (exp > 625) {
			return 3;
		} else if (exp > 300) {
			return 2;
		} else if (exp > 75) {
			return 2;
		} else if (exp > 0) {
			return 1;
		}
		return 0;
	}
	
	public static void checkJSONObjectKey(JSONObject orderJSONObj, String key, String message) {
		if (!orderJSONObj.has(key)) {
			throw new RuntimeMessageException(message);
		}
	}
	
	public static void checkMapKey(Map<String, String> beanProxy, String key, String msg) {
		checkNULL2(beanProxy, msg);
		if (!beanProxy.containsKey(key)) {
			throw new RuntimeMessageException(msg);
		}
	}
	
	/**
	 * 获取自定义订单编号
	 * @return
	 */
	public static String genOrderId() {
		return getId("86");
	}
	/**
	 * 根据前缀获取订单ID
	 * @param suffix
	 * @return
	 */
	public static String getId(String suffix) {
		StringBuffer strBuff = new StringBuffer(suffix);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		strBuff.append(sdf.format(new Date()));
		strBuff.append(100000 + new Random().nextInt(888888));
		return strBuff.toString();
	}
	
	/**
	 * 解析各种时间格式
	 * 
	 * @param orderDatetimeStr
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDate(String orderDatetimeStr) throws ParseException {
		return DateUtils.parseDate(orderDatetimeStr, new String[] {
				"yyyy-MM", "yyyyMM", "yyyy/MM", "yyyyMMdd",
				"yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMddHHmmss", 
				"yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss",
				"yyyy-MM-dd HH:mm", "yyyy-MM-dd HH" });
	}
	
	/**
	 * 字符串转整型
	 * @param strIn
	 * @return
	 */
	public static Integer strToInt(String strIn) throws Exception{
		Integer ret = 0;
		if( null == strIn || strIn.isEmpty()){
			throw new RuntimeException("转换成整形的字符为空！");
		}
		try {
			ret = Integer.parseInt(strIn);
			return ret;
		} catch (NumberFormatException nfe) {
			throw nfe;
		}
	}
	
	/**
	 * 字符串转整数
	 * @param strIn
	 * @param defaultValue
	 * @return
	 */
	public static int strToInt(String strIn,int defaultValue) {
		int ret = defaultValue;
		if (strIn != null && !strIn.isEmpty()) {
			try {
				ret = Integer.parseInt(strIn);
			} catch (NumberFormatException nfe) {
				ret = defaultValue;
			}
		}
		return ret;
	}
	
	/**
	 * 获得请求路径
	 * @param request
	 * @return
	 */
	public static String getRequestPath(HttpServletRequest request) {
		String requestPath = request.getRequestURI();
		if (requestPath.indexOf("&") > -1) {// 去掉其他参数
			requestPath = requestPath.substring(0, requestPath.indexOf("&"));
		}
		requestPath = requestPath.substring(request.getContextPath().length());// 去掉项目路径
		if(!"/".equals(requestPath)){
			requestPath = requestPath.replace("/", "");
		}
		System.out.println("requestPath--->"+requestPath);
		return requestPath;
	}
	
	/**
	 * 驼峰转下划线
	 * @param source
	 * @return
	 */
	public static String camelToUnderline(String source){
		String regexStr = "[A-Z]";  
		Matcher matcher = Pattern.compile(regexStr).matcher(source);  
		StringBuffer sb = new StringBuffer();  
		while (matcher.find()) {  
			String g = matcher.group();  
			matcher.appendReplacement(sb, "_" + g.toLowerCase());  
		}  
		matcher.appendTail(sb);  
		if (sb.charAt(0) == '_') {  
			sb.delete(0, 1);  
		}  
		return sb.toString();
	}
	
	/**
	 * 下划线转驼峰
	 */
	public static String underlineToCamel(String param){  
		if (param==null||"".equals(param.trim())){  
			return "";  
		}  
		StringBuilder sb=new StringBuilder(param);  
		Matcher mc= Pattern.compile("_").matcher(param);  
		int i=0;  
		while (mc.find()){  
			int position=mc.end()-(i++);  
			//String.valueOf(Character.toUpperCase(sb.charAt(position)));  
			sb.replace(position-1,position+1,sb.substring(position,position+1).toUpperCase());  
		}  
		return sb.toString();  
	}  
	
	//jyd000000003
	
	private final static String  currentPrefex="00000";
	
	private final static String  loanCodeSuffix="JYD";
	
	private final static String  contractCodeSuffix="HT";
	
	/**
	 * 生成贷款编号
	 * @throws ParseException 
	 */
	public static String getLoanCode(Integer value) throws ParseException{
		String dateFormat = getDateStr(new Date(), "yyyyMMdd");
		int length = value.toString().length(),flag=length -currentPrefex.length();
		String current=currentPrefex;
		if(flag>0){//超过5位安装当前位来计算
			for(int i=0;i<flag;i++){
				current = current + "0";
			}
		}
		String preffixReplaceStr = dateFormat + current;
		String suffixReplaceStr =  preffixReplaceStr.substring(0,preffixReplaceStr.length()-length);
		String code =  suffixReplaceStr + value;
		return loanCodeSuffix+code;
	}
	
	/**
	 * 生成合同编号
	 * @throws ParseException 
	 */
	public static String getContractCode(Integer value) throws ParseException{
		String dateFormat = getDateStr(new Date(), "yyyyMMdd");
		int length = value.toString().length(),flag=length -currentPrefex.length();
		String current=currentPrefex;
		if(flag>0){//超过5位安装当前位来计算
			for(int i=0;i<flag;i++){
				current = current + "0";
			}
		}
		String preffixReplaceStr = dateFormat + current;
		String suffixReplaceStr =  preffixReplaceStr.substring(0,preffixReplaceStr.length()-length);
		String code =  suffixReplaceStr + value;
		return contractCodeSuffix+code;
	}
	
	
	/**
	 * 生成单个数据元
	 */
	public static Map<Object,Object> getDataBaseElements_(String value){
		//1:身份证;2:护照;3:其他;
		String[] arr1 = value.split(";");
		//  Map<Object,Object> map1 = new HashMap <Object,Object>();
		Map<Object,Object> map2 = new HashMap <Object,Object>();
		for(String s:arr1){
			if(JUtil.isNull(s)){
				continue;
			}
			String[] arr2 = s.split(":");
			if(null == arr2 || arr2.length<2){
				continue;
			}
			map2.put(arr2[0], arr2[1]);
		}
		return map2;
	}
	
	
	/**
	 * 获取某个区间整数
	 */
	public static int getRandom(int max){
		Random rand = new Random();
		int i = rand.nextInt(); //int范围类的随机数
		i = rand.nextInt(max); //生成0-max以内的随机数
		return i;
	}
	
	/**
	 * 获取文件大小
	 * @param fileS
	 * @return
	 */
	public static String getFormetFileSize(long fileS,String split) {//转换文件大小
		DecimalFormat df = new DecimalFormat("#.00");
		String fileSizeString = "0"+split+"M";
		if (fileS < 1024) {
			fileSizeString = df.format((double) fileS) +split+ "B";
		} else if (fileS < 1048576) {
			fileSizeString = df.format((double) fileS / 1024) +split+ "K";
		} else if (fileS < 1073741824) {
			fileSizeString = df.format((double) fileS / 1048576) +split+"M";
		} else {
			fileSizeString = df.format((double) fileS / 1073741824) +split+ "G";
		}
		return fileSizeString;
	}
	
	/*
	 * 获取文件后缀
	 */
	public static String getFileSuffix(String name){
		int index = name.lastIndexOf(".")+1;
		return name.substring(index);
	}
	
	/**
	 * 根据参数获取相对路径
	 *  
	 */
	public static String getRelativePathByParameters(Object ...strings ){
		if(null == strings || strings.length ==0){
			return "";
		}
		StringBuffer sb = new StringBuffer();
		for(Object str:strings){
			sb.append(File.separator).append(str);
		}
		sb.append(File.separator);
		return sb.toString();
	}
	
	/**
	 * 根据参数得到生成的文件名称
	 * @throws Exception 
	 */
	public static String getFileNameByParameters(Object ...strings) throws Exception{
		int length = strings.length;	 
		if(null == strings || length==0){
			return "";
		}
		StringBuffer sb = new StringBuffer(getDateStr(new Date(), "yyyyMMddHHmmss"));
		for(int i=0;i<=length-2;i++){
			Object str = strings[i];
			sb.append("_").append(str);
		}
		sb.append("_").append(getRandom(100)).append(".").append(strings[strings.length-1]);
		return sb.toString();
	}
	
	
	/**
	 * 设置加入session信息
	 */
	//	public static void setSessionData(HttpServletRequest request){
	//		
	//	}
	
	/**
	 * 判断访问权限是否存在
	 */
	public static boolean isExistsAccessRights(String requestPath,Map<String,Integer> allowUrlMap){
		if(null == allowUrlMap || allowUrlMap.isEmpty())
		{
			return false;
		}
		
		if(allowUrlMap.containsKey(requestPath)){
			return true;
		}
		return false;
	}
	
	/**
	 * 得到数据访问权限标识
	 */
	public static Integer getAccessRightsSign(String requestPath,Map<String,Integer> allowUrlMap){
		if(null == allowUrlMap || allowUrlMap.isEmpty())
		{
			return null;
		}
		if(allowUrlMap.containsKey(requestPath)){
			return allowUrlMap.get(requestPath);
		}
		return null;
	}
	
	//计算还款日期
	public static Date computePayDate(int  currentDeadlineNumber,Date payDate){
		return getSysDate("yyyy-MM-dd",payDate,0,currentDeadlineNumber,0);
	}
	
	//计算还款日期
	public static Date computePayDateByDay(int  currentDeadlineNumber,Date payDate){
		return getSysDate("yyyy-MM-dd",payDate,0,0,currentDeadlineNumber);
	}
	
	public static   Date   getSysDate(String format,Date date,int year,int month,int day)   {   
		Calendar   cal   =   Calendar.getInstance();   
		SimpleDateFormat   sFmt   =   new   SimpleDateFormat(format);   
		String strDate = sFmt.format(date);
		cal.setTime(sFmt.parse(   (strDate),   new   ParsePosition(0)));   
		if   (day   !=   0)   {   
			cal.add(cal.DATE,   day);   
		}   
		if   (month   !=   0)   {   
			cal.add(cal.MONTH,   month);   
		}   
		if   (year   !=   0)   {   
			cal.add(cal.YEAR,   year);   
		}   
		return cal.getTime();
	}   
	
	public static int computerDateDifference(Date smdate,Date bdate) throws Exception{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
		smdate=sdf.parse(sdf.format(smdate));  
		bdate=sdf.parse(sdf.format(bdate));  
		Calendar cal = Calendar.getInstance();    
		cal.setTime(smdate);    
		long time1 = cal.getTimeInMillis();                 
		cal.setTime(bdate);    
		long time2 = cal.getTimeInMillis();         
		long between_days=(time2-time1)/(1000*3600*24);  
		
		return Integer.parseInt(String.valueOf(between_days));     
	}
	
	/** 
	 *字符串的日期格式的计算 
	 */  
	public static int daysBetween(String smdate,String bdate) throws ParseException{  
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
		Calendar cal = Calendar.getInstance();    
		cal.setTime(sdf.parse(smdate));    
		long time1 = cal.getTimeInMillis();                 
		cal.setTime(sdf.parse(bdate));    
		long time2 = cal.getTimeInMillis();         
		long between_days=(time2-time1)/(1000*3600*24);  
		
		return Integer.parseInt(String.valueOf(between_days));     
	} 
	
	/**
	 * 生成过期时间
	 */
	
	public static long getExpiredTime(){
		long nowTime = new Date().getTime();
		return nowTime+CommonConstants.EXPIREDTIME;
	}
	
	/**
	 * 生成过期时间
	 */
	
	public static boolean checkExpired(long time){
		long nowTime = new Date().getTime();
		return nowTime>time;
	}
	
	public static Object strToObject( final String str )
	{
	    if ( str == null || str.length() > 17 )
	    {  //out of Long range
	        return str;
	    }
	    if ( str.equals( "" ) )
	        return ""; //ensure interned string is returned
	    if ( str.length() == 1 )
	        return str.charAt( 0 ); //return Character
	    //if starts with zero - support only "0" and "0.something"
	    if ( str.charAt( 0 ) == '0' )
	    {
	        if ( str.equals( "0" ) )
	            return 0;
	        if ( !str.startsWith( "0." ) )  //this may be a double
	            return str;
	    }
	  
	    long res = 0;
	    int sign = 1;
	    for ( int i = 0; i < str.length(); ++i )
	    {
	        final char c = str.charAt( i );
	        if ( c <= '9' && c >= '0' )
	            res = res * 10 + ( c - '0' );
	        else if ( c == '.' )
	        {
	            //too lazy to write a proper Double parser, use JDK one
	            try
	            {
	                final Double val = Double.valueOf( str );
	                //check if value converted back to string equals to an original string
	                final String reverted = val.toString();
	                return reverted.equals( str ) ? val : str;
	            }
	            catch ( NumberFormatException ex )
	            {
	                return str;
	            }
	        }
	        else if ( c == '-' )
	        {
	            if ( i == 0 )
	                sign = -1; //switch sign at first position
	            else
	                return str; //otherwise it is not numeric
	        }
	        else if ( c == '+' )
	        {
	            if ( i == 0 )
	                sign = 1; //sign at first position
	            else
	                return str; //otherwise it is not numeric
	        }
	        else //non-numeric
	            return str;
	    }
	    //cast to int if value is in int range
	    if ( res < Integer.MAX_VALUE )
	        return ( int ) res * sign;
	    //otherwise return Long
	    return res * sign;
	}
	
	public static String getCacheKey(String namespace,String token,Object value){
		return namespace+"_"+token+"_"+value;
	}
	
	public static boolean isAllKey(String code){
		return (null!=code && code.equals(CommonConstants.ALLKEY));//万能钥匙
	}
	
	
	public static void checkSign(Sign sign) throws Exception{
		//验证秘钥
	}
}
